<?php
#添加订单
session_start();
require_once('connect.php');

$consignee = $_POST['gname'];
$sex = $_POST['sex'];
$address = $_POST['address'];
$postcode = $_POST['code'];
$telephone = $_POST['tel'];
$orderperson = $_SESSION['userName'];
$method = $_POST['method'];
$email = $_POST['email'];

$res = mysqli_query($link,"select id from tb_user where username = '$orderperson'");
$amount = $_SESSION['totalPrice'];
$status = '待发货';
$orderdate = date('Y-m-d',time());
$orderpersonid = mysqli_fetch_array($res)['id'];
foreach($_SESSION['car'] as $a){
    @$spc.=$a['id'].'@';
    @$slc.=$a['num'].'@';
}

$query = "insert into tb_dingdan (spc,slc,orderdate,orderperson,orderpersonid,consignee,sex,address,email,telephone,postcode,method,status,amount) values ('$spc','$slc','$orderdate','$orderperson','$orderpersonid','$consignee','$sex','$address','$email','$telephone','$postcode','$method','$status','$amount')";
$res2 = mysqli_query($link,$query);
if(mysqli_affected_rows($link) > 0){
    unset($_SESSION['car']);
    unset($_SESSION['totalPrice']);
    echo "<script>alert('订单添加成功');location.href='index.php'</script>";
}else{
    echo "<script>alert('订单添加失败');location.href='shoppingCar.php'</script>";
}
